package com.springboot.config.tool;

import com.springboot.project.common.entity.User;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * Created with IntelliJ IDEA.
 * User: lby.
 * Date: 2018/4/6.
 * Version: V1.0.
 */
public class ToolUtil {

    /**
     *
     * @param o
     * @return
     */
    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    /**
     * 根据角色Id字符串查找角色Id
     * @param roleIdStr
     * @return
     */
    public static List<Integer> getRoleId(String roleIdStr)
    {
        List<Integer> roleIds = new ArrayList<Integer>();
        //判断是否为多个角色
        if(roleIdStr.contains(","))
        {
            String[] roleIdchar = roleIdStr.split(",");
            for(int i = 0; i < roleIdchar.length; i++)
            {
                roleIds.add(Integer.parseInt(roleIdchar[i]));
            }
        }
        else
        {
            roleIds.add(Integer.parseInt(roleIdStr));
        }
        return roleIds;
    }

    /**
     * @param credentials 密码
     * @param salt 密码盐
     * @return
     */
    public static String md5(String credentials, String salt) {
        int hashIterations = 1024;//加密的次数
        String hashAlgorithmName = "MD5";//加密方式
        String data = "123456";
        Object simpleHash = new SimpleHash(hashAlgorithmName, data, salt, hashIterations);
        String tokenCredentials = String.valueOf(simpleHash);
        return tokenCredentials;
    }

    /**
     * 设置用户默认的头像
     */
    public static void SetUpUserDefault(User user)
    {
        user.setHeadimage("girl.gif");
        user.setSalt("zdog");
        user.setStatus(1);
        user.setCreatetime(new Date());
        //MD5加密
        user.setPassword(md5(user.getPassword(),user.getSalt()));
    }

    /**
     * 转换为Integer数组<br>
     * @return 结果
     */
    public static Integer[] toIntArray(String str) {
        if (str == null || str.length() == 0) {
            return new Integer[] {};
        }
        String[] arr = str.split(",");
        final Integer[] ints = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) {
            final Integer v = Integer.parseInt(arr[i]);
            ints[i] = v;
        }
        return ints;
    }

    //获取请求HttpServletRequest
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * 获取IP地址
     * 使用Nginx等反向代理软件， 则不能通过request.getRemoteAddr()获取IP地址
     * 如果使用了多级反向代理的话，X-Forwarded-For的值并不止一个，而是一串IP地址，X-Forwarded-For中第一个非unknown的有效IP字符串，则为真实IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
}
